
using Youz.Common.DBUtility;
using Youz.Common.Entity;
using Youz.CreditRepay.Entity;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using Youz.Common.Helper;

namespace Youz.CreditRepay.DAL
{
    /// <summary>
    /// Member_Withdraw Data Class
    /// </summary>
    public partial class Member_WithdrawDAL : BaseDAL
    {
        private DbHelperSQL DbHelperSQL = new DbHelperSQL();
        public string TableName = "Member_Withdraw";

        public Member_WithdrawDAL()
        { }

        #region  成员方法
        /// <summary>
        /// 生成ID
        /// </summary>
        /// <returns></returns>
        public string GenerateID()
        {
            var id = TypeHelper.ConvertTo<Int64>(DbHelperSQL.GetSingle("select isnull(max(WithdrawID),0) from [dbo].[Member_Withdraw]"));

            return (id == 0 ? 80000000 : id + 1).ToString();
        }

        public bool Add(Member_Withdraw model)
        {
            var sqlParams = GetAddSql(model);
            return DbHelperSQL.ExecuteSql(sqlParams.Sql, sqlParams.SqlParameters) == 1;
        }

        public SqlAndParams GetAddSql(Member_Withdraw model)
        {
            StringBuilder strSql = new StringBuilder();

            strSql.Append("insert into [Member_Withdraw](");
            strSql.Append("[WithdrawID],[MemberAccount],[MoneyType],[PayAmount],[WithdrawAmount],[WithdrawFee],[PayBankCode],[PayBankName],[PayBankMobileNo],[PayBankAccountName],[PayBankAccountNo],[PayIDNo],[BankCode],[BankName],[BankAccountName],[BankAccountNo],[BankCountry],[BankProvince],[BankCity],[BankPosition],[Address],[IsSettlement],[Memo],[Status],[CreateTime],[ModifyTime]");
            strSql.Append(") values (");
            strSql.Append("@WithdrawID,@MemberAccount,@MoneyType,@PayAmount,@WithdrawAmount,@WithdrawFee,@PayBankCode,@PayBankName,@PayBankMobileNo,@PayBankAccountName,@PayBankAccountNo,@PayIDNo,@BankCode,@BankName,@BankAccountName,@BankAccountNo,@BankCountry,@BankProvince,@BankCity,@BankPosition,@Address,@IsSettlement,@Memo,@Status,@CreateTime,@ModifyTime");
            strSql.Append(")");

            SqlParameter[] parameters = {

                    new SqlParameter("@WithdrawID", model.WithdrawID),
                    new SqlParameter("@MemberAccount", model.MemberAccount),
                    new SqlParameter("@MoneyType", model.MoneyType),
                    new SqlParameter("@PayAmount", model.PayAmount),
                    new SqlParameter("@WithdrawAmount", model.WithdrawAmount),
                    new SqlParameter("@WithdrawFee", model.WithdrawFee),
                    new SqlParameter("@PayBankCode", model.PayBankCode),
                    new SqlParameter("@PayBankName", model.PayBankName),
                    new SqlParameter("@PayBankMobileNo", model.PayBankMobileNo),
                    new SqlParameter("@PayBankAccountName", model.PayBankAccountName),
                    new SqlParameter("@PayBankAccountNo", model.PayBankAccountNo),
                    new SqlParameter("@PayIDNo", model.PayIDNo),
                    new SqlParameter("@BankCode", model.BankCode),
                    new SqlParameter("@BankName", model.BankName),
                    new SqlParameter("@BankAccountName", model.BankAccountName),
                    new SqlParameter("@BankAccountNo", model.BankAccountNo),
                    new SqlParameter("@BankCountry", model.BankCountry),
                    new SqlParameter("@BankProvince", model.BankProvince),
                    new SqlParameter("@BankCity", model.BankCity),
                    new SqlParameter("@BankPosition", model.BankPosition),
                    new SqlParameter("@Address", model.Address),
                    new SqlParameter("@IsSettlement", model.IsSettlement),
                    new SqlParameter("@Memo", model.Memo),
                    new SqlParameter("@Status", model.Status),
                    new SqlParameter("@CreateTime", DateTime.Now),
                    new SqlParameter("@ModifyTime", DateTime.Now)
            };

            return new SqlAndParams() { Sql = strSql.ToString(), SqlParameters = parameters };
        }

        /// <summary>
        /// 提现申请
        /// </summary>
        /// <param name="contract"></param>
        /// <returns></returns>
        public bool Withdraw(Member_Withdraw model)
        {
            List<SqlAndParams> dict = new List<SqlAndParams>();
            //Guid ObjectID = Guid.NewGuid();

            #region  提现表
            dict.Add(GetAddSql(model));
            #endregion

            //提现金额
            var listHistory = new List<AssetHistoryItem>();
            listHistory.Add(new AssetHistoryItem(AssetOperateType.Minus, AssetHistoryType.Balance, model.PayAmount));
            dict.Add(new SqlAndParams { Sql = new Member_AssetHistoryDAL().GetSqlByOperateAsset(model.MemberAccount, model.MemberAccount, model.WithdrawID, AssetHistoryChangeType.Withdraw, listHistory) });

            return DbHelperSQL.ExecuteSqlTran(dict);
        }
        /// <summary>
        /// 撤销提现
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool RevocationWithdraw(Member_Withdraw model, Member_AssetHistory assetHistoryModel, Sys_StatusRecord recordModel)
        {
            List<SqlAndParams> dict = new List<SqlAndParams>();

            #region 提现表
            StringBuilder strSql = new StringBuilder();

            strSql.Append("update [Member_Withdraw] set ");
            strSql.Append("[Status]=@Status ");
            strSql.Append(" where [WithdrawID]=@WithdrawID");
            SqlParameter[] parameters = {	  
					new SqlParameter("@Status", model.Status),
                    new SqlParameter("@WithdrawID", model.WithdrawID)
			};
            dict.Add(new SqlAndParams() { Sql = strSql.ToString(), SqlParameters = parameters });
            #endregion

            #region  资产表
            StringBuilder strSql1 = new StringBuilder();
            strSql1.Append("update [Member_Account] set ");
            strSql1.Append("[TodayOutMoney]=TodayOutMoney-@TodayOutMoney,[ModifyTime]=@ModifyTime");
            strSql1.Append(" where [MemberAccount]=@MemberAccount");

            SqlParameter[] parameters1 = {                 
                    new SqlParameter("@MemberAccount", model.MemberAccount),                   
                    new SqlParameter("@TodayOutMoney", model.WithdrawAmount+model.WithdrawFee),
                    new SqlParameter("@ModifyTime", model.CreateTime)
            };
            dict.Add(new SqlAndParams() { Sql = strSql1.ToString(), SqlParameters = parameters1 });
            #endregion

            #region 资产变更表
            StringBuilder strSql2 = new StringBuilder();

            strSql2.Append("insert into [Member_AssetHistory](");
            strSql2.Append("[MemberAccount],[AssetType],[ChangeType],[ChangeAgoBalance],[ChangeBalance],[ChangeAfterBalance],[Memo],[ObjectID],[CreateTime],[Creator]");
            strSql2.Append(") values (");
            strSql2.Append("@MemberAccount,@AssetType,@ChangeType,@ChangeAgoBalance,@ChangeBalance,@ChangeAfterBalance,@Memo,@ObjectID,@CreateTime,@Creator");
            strSql2.Append(")");

            SqlParameter[] parameters2 = {
                  
                   // new SqlParameter("@HistoryID", assetHistoryModel.HistoryID),
                    new SqlParameter("@MemberAccount", assetHistoryModel.MemberAccount),
                    new SqlParameter("@AssetType", assetHistoryModel.AssetType),
                    new SqlParameter("@ChangeType", assetHistoryModel.ChangeType),
                    new SqlParameter("@ChangeAgoBalance", assetHistoryModel.ChangeAgoBalance),
                    new SqlParameter("@ChangeBalance", assetHistoryModel.ChangeBalance),
                    new SqlParameter("@ChangeAfterBalance", assetHistoryModel.ChangeAfterBalance),
                    new SqlParameter("@Memo", assetHistoryModel.Memo),
                    new SqlParameter("@ObjectID", assetHistoryModel.ObjectID),
                    new SqlParameter("@CreateTime", assetHistoryModel.CreateTime),
                    new SqlParameter("@Creator", assetHistoryModel.Creator)
            };
            dict.Add(new SqlAndParams() { Sql = strSql2.ToString(), SqlParameters = parameters2 });
            #endregion

            #region 状态变更表
            StringBuilder strSql3 = new StringBuilder();
            strSql3.Append("insert into [Sys_StatusRecord](");
            strSql3.Append("[RecordId],[MemberAccount],[RecordTable],[RecordType],[BeforeValue],[AfterValue],[CreateTime],[Creator],[Memo],[ObjectID],[LoginID]");
            strSql3.Append(") values (");
            strSql3.Append("@RecordId,@MemberAccount,@RecordTable,@RecordType,@BeforeValue,@AfterValue,@CreateTime,@Creator,@Memo,@ObjectID,@LoginID");
            strSql3.Append(")");

            SqlParameter[] parameters3 = {			  
					new SqlParameter("@RecordId", recordModel.RecordId),
					new SqlParameter("@MemberAccount", recordModel.MemberAccount),
					new SqlParameter("@RecordTable", recordModel.RecordTable),
					new SqlParameter("@RecordType", recordModel.RecordType),
					new SqlParameter("@BeforeValue", recordModel.BeforeValue),
					new SqlParameter("@AfterValue", recordModel.AfterValue),
					new SqlParameter("@CreateTime", recordModel.CreateTime),
					new SqlParameter("@Creator", recordModel.Creator),
					new SqlParameter("@Memo", recordModel.Memo),
					new SqlParameter("@ObjectID", recordModel.ObjectID),
					new SqlParameter("@LoginID", recordModel.LoginID)
			};
            dict.Add(new SqlAndParams() { Sql = strSql3.ToString(), SqlParameters = parameters3 });
            #endregion

            return DbHelperSQL.ExecuteSqlTran(dict);
        }
        /// <summary>
        ///  保存一条数据
        /// </summary>
        public bool Update(Member_Withdraw model)
        {
            List<SqlAndParams> dict = new List<SqlAndParams>();
            StringBuilder strSql = new StringBuilder();

            strSql.Append("update [Member_Withdraw] set ");
            strSql.Append("[MemberAccount]=@MemberAccount,[MoneyType]=@MoneyType,[WithdrawAmount]=@WithdrawAmount,[WithdrawFee]=@WithdrawFee,[BankName]=@BankName,[BankAccountNo]=@BankAccountNo,[BankAccountName]=@BankAccountName,[BankPosition]=@BankPosition,[BankCountry]=@BankCountry,[Address]=@Address,[CreateTime]=@CreateTime,[Status]=@Status,[Memo]=@Memo,[ModifyTime]=@ModifyTime");
            strSql.Append(" where [WithdrawID]=@WithdrawID");

            SqlParameter[] parameters = {
				  
					new SqlParameter("@WithdrawID", model.WithdrawID),
					new SqlParameter("@MemberAccount", model.MemberAccount),
					new SqlParameter("@MoneyType", model.MoneyType),
					new SqlParameter("@WithdrawAmount", model.WithdrawAmount),
					new SqlParameter("@WithdrawFee", model.WithdrawFee),
					new SqlParameter("@BankName", model.BankName),
					new SqlParameter("@BankAccountNo", model.BankAccountNo),
					new SqlParameter("@BankAccountName", model.BankAccountName),
					new SqlParameter("@BankPosition", model.BankPosition),
					new SqlParameter("@BankCountry", model.BankCountry),
					new SqlParameter("@Address", model.Address),
					new SqlParameter("@CreateTime", model.CreateTime),
					new SqlParameter("@Status", (int)model.Status),
					new SqlParameter("@Memo", model.Memo),
					new SqlParameter("@ModifyTime", model.ModifyTime)
			};
            dict.Add(new SqlAndParams() { Sql = strSql.ToString(), SqlParameters = parameters });

            return DbHelperSQL.ExecuteSqlTran(dict);
        }

        public bool Update(string id, WithdrawStatus Status)
        {
            StringBuilder strSql = new StringBuilder();

            strSql.Append("update [Member_Withdraw] set ");
            strSql.Append("[Status]=@Status ");
            strSql.Append(" where [WithdrawID]=@WithdrawID");

            SqlParameter[] parameters = {
                    new SqlParameter("@WithdrawID", id),
                    new SqlParameter("@Status", (int)Status)
            };

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        ///  审核
        /// </summary>
        public bool Audit(string withdrawID, WithdrawStatus status, string auditOpinion)
        {
            SqlParameter param = new SqlParameter("@return", SqlDbType.Int);
            param.Direction = ParameterDirection.ReturnValue;

            DbHelperSQL.RunProcedureNonQuery("up_AuditWithdraw", new IDataParameter[] {
                param,
                new SqlParameter("@WithdrawID", withdrawID),
                new SqlParameter("@Status", (int)status),
                new SqlParameter("@AuditOpinion", auditOpinion) });

            return param.Value.ToString() == "1";
        }

        /// <summary>
        /// 删除一条数据
        ///</summary>
        public bool Delete(String WithdrawID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from [Member_Withdraw] ");
            strSql.Append(" where [WithdrawID]=@WithdrawID");

            SqlParameter[] parameters = {
			
				new SqlParameter("@WithdrawID", WithdrawID)             
			  
			};

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Member_Withdraw Get(String WithdrawID)
        {
            StringBuilder strSql = new StringBuilder();

            strSql.Append("select  top 1 * from [Member_Withdraw] ");
            strSql.Append(" where [WithdrawID]=@WithdrawID");

            SqlParameter[] parameters = {
			
				new SqlParameter("WithdrawID", WithdrawID)             
			  
			};

            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return Get(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Member_Withdraw Get(DataRow row)
        {
            if (row == null) return null;

            Member_Withdraw model = new Member_Withdraw();

            if (row["WithdrawID"] != DBNull.Value)
            {
                model.WithdrawID = (String)row["WithdrawID"];
            }

            if (row["MemberAccount"] != DBNull.Value)
            {
                model.MemberAccount = (String)row["MemberAccount"];
            }

            if (row["MoneyType"] != DBNull.Value)
            {
                model.MoneyType = (String)row["MoneyType"];
            }

            if (row["PayAmount"] != DBNull.Value)
            {
                model.PayAmount = (Decimal)row["PayAmount"];
            }

            if (row["WithdrawAmount"] != DBNull.Value)
            {
                model.WithdrawAmount = (Decimal)row["WithdrawAmount"];
            }

            if (row["WithdrawFee"] != DBNull.Value)
            {
                model.WithdrawFee = (Decimal)row["WithdrawFee"];
            }

            if (row["PayBankCode"] != DBNull.Value)
            {
                model.PayBankCode = (String)row["PayBankCode"];
            }

            if (row["PayBankName"] != DBNull.Value)
            {
                model.PayBankName = (String)row["PayBankName"];
            }

            if (row["PayBankMobileNo"] != DBNull.Value)
            {
                model.PayBankMobileNo = (String)row["PayBankMobileNo"];
            }

            if (row["PayBankAccountName"] != DBNull.Value)
            {
                model.PayBankAccountName = (String)row["PayBankAccountName"];
            }

            if (row["PayBankAccountNo"] != DBNull.Value)
            {
                model.PayBankAccountNo = (String)row["PayBankAccountNo"];
            }

            if (row["PayIDNo"] != DBNull.Value)
            {
                model.PayIDNo = (String)row["PayIDNo"];
            }

            if (row["BankCode"] != DBNull.Value)
            {
                model.BankCode = (String)row["BankCode"];
            }

            if (row["BankName"] != DBNull.Value)
            {
                model.BankName = (String)row["BankName"];
            }

            if (row["BankAccountName"] != DBNull.Value)
            {
                model.BankAccountName = (String)row["BankAccountName"];
            }

            if (row["BankAccountNo"] != DBNull.Value)
            {
                model.BankAccountNo = (String)row["BankAccountNo"];
            }

            if (row["BankCountry"] != DBNull.Value)
            {
                model.BankCountry = (String)row["BankCountry"];
            }

            if (row["BankProvince"] != DBNull.Value)
            {
                model.BankProvince = (String)row["BankProvince"];
            }

            if (row["BankCity"] != DBNull.Value)
            {
                model.BankCity = (String)row["BankCity"];
            }

            if (row["BankPosition"] != DBNull.Value)
            {
                model.BankPosition = (String)row["BankPosition"];
            }

            if (row["Address"] != DBNull.Value)
            {
                model.Address = (String)row["Address"];
            }

            if (row["IsSettlement"] != DBNull.Value)
            {
                model.IsSettlement = (Boolean)row["IsSettlement"];
            }

            if (row["Memo"] != DBNull.Value)
            {
                model.Memo = (String)row["Memo"];
            }

            if (row["Status"] != DBNull.Value)
            {
                model.Status = (WithdrawStatus)(Int32)row["Status"];
            }

            if (row["CreateTime"] != DBNull.Value)
            {
                model.CreateTime = (DateTime)row["CreateTime"];
            }

            if (row["ModifyTime"] != DBNull.Value)
            {
                model.ModifyTime = (DateTime)row["ModifyTime"];
            }

            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet Gets(string sqlWhere)
        {
            StringBuilder strSql = new StringBuilder();

            strSql.Append("select * from [Member_Withdraw] ");
            if (sqlWhere.Trim() != "")
            {
                strSql.Append(" where " + sqlWhere);
            }

            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet Gets(int Top, string sqlWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" *  FROM [Member_Withdraw] ");

            if (sqlWhere.Trim() != "")
            {
                strSql.Append(" where " + sqlWhere);
            }
            if (filedOrder.Trim() != "")
            {
                strSql.Append(" order by " + filedOrder);
            }

            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string sqlWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM [Member_Withdraw] ");
            if (sqlWhere.Trim() != "")
            {
                strSql.Append(" where " + sqlWhere);
            }
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 获取提现之和
        /// </summary>
        public decimal GetWithdrawSum(string sqlWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select SUM(WithdrawAmount) FROM [Member_Withdraw] ");
            if (sqlWhere.Trim() != "")
            {
                strSql.Append(" where " + sqlWhere);
            }
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToDecimal(obj);
            }
        }

        /// <summary>
        /// 获得数据列表分页
        /// </summary>
        public DataTable Gets(string sqlWhere, int pageindex, int pagesize, out int total, params SqlParameter[] cmdParms)
        {
            StringBuilder strSql = new StringBuilder();

            strSql.Append(@" select a.*,b.Memo as StatusRecordMemo,c.CurrencyID,c.CurrencyName,d.Status MemberAccountStatus
                            from Member_Withdraw　a
                            left join [Sys_StatusRecord]　b on a.WithdrawID=b.ObjectID 
                            and b.CreateTime = (select max(CreateTime) from [Sys_StatusRecord] where a.WithdrawID=[Sys_StatusRecord].ObjectID)  
                            left join [Sys_Currency] c on a.MoneyType = c.CurrencyID   
							left join Member_Info d on a.MemberAccount=d.MemberAccount ");
            if (sqlWhere.Trim() != "")
            {
                strSql.Append(" where " + sqlWhere);
            }

            int count = 0;
            DataTable dt = DbHelperSQL.GetPager(pageindex, pagesize, "" + strSql + "", "order by createTime desc ", out count, cmdParms);
            total = count;
            return dt;
        }
        #endregion  成员方法

        /// <summary>
        /// 按日期统计提现金额和数量
        /// </summary>
        /// <returns></returns>
        public DataTable GetWithdrawChart()
        {
            StringBuilder strSql = new StringBuilder();

            strSql.Append(@"select [Date]=convert(varchar(10), CreateTime, 111),Amount=sum(WithdrawAmount),[Count]=count(0) 
                        from [dbo].[Member_Withdraw]
                        where [Status]=4
                        and convert(varchar(10), CreateTime, 111) >= convert(varchar(10), getdate()-30, 111) 
                        group by convert(varchar(10), CreateTime, 111)");

            DataSet ds = DbHelperSQL.Query(strSql.ToString());
            DataTable dt = ds.Tables[0];
            return dt;
        }

        #region 易支付代付接口
        public bool SubmitPayEase()
        {
            List<SqlAndParams> dict = new List<SqlAndParams>();
            WithdrawStatus oldStatus = WithdrawStatus.Pending, newStatus = WithdrawStatus.Review;

            SqlParameter[] parameters = {
                new SqlParameter("@OldStatus", oldStatus),
                new SqlParameter("@NewStatus", newStatus)
            };
            dict.Add(new SqlAndParams() { Sql = "update Member_Withdraw set [Status]=@NewStatus,ModifyTime=getdate() where [Status]=@OldStatus", SqlParameters = parameters });

            #region 状态变更表
            StringBuilder strSql2 = new StringBuilder();
            strSql2.Append("insert into [Sys_StatusRecord](");
            strSql2.Append("[RecordId],[MemberAccount],[RecordTable],[RecordType],[BeforeValue],[AfterValue],[CreateTime],[Creator],[Memo],[ObjectID],[LoginID]");
            strSql2.Append(")");
            strSql2.Append(" select NEWID(),MemberAccount,'Member_Withdraw','Status',[Status],@NewStatus,GETDATE(),'pay','',WithdrawID,null from Member_Withdraw where [Status]=@OldStatus");

            dict.Add(new SqlAndParams() { Sql = strSql2.ToString(), SqlParameters = parameters });
            #endregion

            return DbHelperSQL.ExecuteSqlTran(dict);
        }

        public bool SubmitPayEaseFinish(String startWithdrawID, string endWithdrawID)
        {
            List<SqlAndParams> dict = new List<SqlAndParams>();
            WithdrawStatus oldStatus = WithdrawStatus.Review, newStatus = WithdrawStatus.Submited;

            SqlParameter[] parameters = {
                new SqlParameter("@StartWithdrawID", startWithdrawID),
                new SqlParameter("@EndWithdrawID", endWithdrawID),
                new SqlParameter("@OldStatus", oldStatus),
                new SqlParameter("@NewStatus", newStatus)
            };
            dict.Add(new SqlAndParams() { Sql = "update Member_Withdraw set [Status]=@NewStatus,ModifyTime=getdate() where [WithdrawID]<=@EndWithdrawID and [WithdrawID]>=@StartWithdrawID and [Status]=@OldStatus", SqlParameters = parameters });

            #region 状态变更表
            StringBuilder strSql2 = new StringBuilder();
            strSql2.Append("insert into [Sys_StatusRecord](");
            strSql2.Append("[RecordId],[MemberAccount],[RecordTable],[RecordType],[BeforeValue],[AfterValue],[CreateTime],[Creator],[Memo],[ObjectID],[LoginID]");
            strSql2.Append(")");
            strSql2.Append(" select NEWID(),MemberAccount,'Member_Withdraw','Status',[Status],@NewStatus,GETDATE(),'pay','',WithdrawID,null from Member_Withdraw where [WithdrawID]<=@EndWithdrawID and [WithdrawID]>=@StartWithdrawID and [Status]=@OldStatus");

            dict.Add(new SqlAndParams() { Sql = strSql2.ToString(), SqlParameters = parameters });
            #endregion

            return DbHelperSQL.ExecuteSqlTran(dict);
        }

        public bool SavePayEaseLog(string id, int status, string desc, decimal fee, DateTime? paydate, string transdesc)
        {
            /*
            0－未处理，
            1－已成功
            2－处理中
            3－已失败
            4 - 待处理（自动处理程序状态）
            8 - 没有该用户标识对应的代付记录
            9－查询失败
            */
            var sql = @"
                if exists(select * from [dbo].[Pay_PayEaseWithdraw] where WithdrawID=@WithdrawID)
                begin
                    if not exists(select * from [dbo].[Pay_PayEaseWithdraw] where WithdrawID=@WithdrawID and [Status] in(1,3))
                        UPDATE [dbo].[Pay_PayEaseWithdraw] set [Status]=@Status,[StatusDesc]=@StatusDesc,[Fee]=@Fee,[PayDate]=@PayDate,[TransDesc]=@TransDesc,ModifyTime=getdate() where WithdrawID=@WithdrawID
                end
                else
                begin
                    INSERT INTO [dbo].[Pay_PayEaseWithdraw]
                       ([WithdrawID],[Status],[StatusDesc],[Fee],[PayDate],[TransDesc],[CreateTime],[ModifyTime])
                        VALUES(@WithdrawID,@Status,@StatusDesc,@Fee,@PayDate,@TransDesc,getdate(),getdate())
                end";
            DbHelperSQL.ExecuteSql(sql, new SqlParameter[] {
                new SqlParameter("@WithdrawID", id),
                new SqlParameter("@Status", status),
                new SqlParameter("@StatusDesc", desc),
                new SqlParameter("@Fee", fee),
                new SqlParameter("@PayDate", paydate),
                new SqlParameter("@TransDesc", transdesc),});
            return true;
        } 
        #endregion
    }
}


